package com.wen.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.wen.moudle.Account;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;


// 账号管理
public class AccountManager {
    public static HashMap<String, Account> accounts;
    private static final File AccountPath = new File("account.properties");

    private AccountManager( ){ }

    // 读历史账号
    public static HashMap<String, Account> readAccount(){
        if (accounts != null) return accounts;
        // 配置文件不存在,返回一个空
        if (!FileUtil.exist(AccountPath)) {
            accounts = new HashMap<>();
            return accounts;
        }
        //  配置文件存在,序列化读取配置文件
        BufferedInputStream stream = FileUtil.getInputStream(AccountPath);
        accounts = IoUtil.readObj(stream);
        IoUtil.close(stream);
        return accounts;
    }

    // 保存账号
    public static void saveAccount(Account account){
        accounts = accounts == null ? readAccount() : accounts;
        accounts.put(account.username,account);
        // 写入配置文件
        //  配置文件存在,序列化读取配置文件
        BufferedOutputStream outputStream = FileUtil.getOutputStream(AccountPath);
        IoUtil.writeObj(outputStream,true,accounts);
        IoUtil.close(outputStream);
    }

    // 删除账号
    public static void deleteAccount(Account account){
        accounts = accounts == null ? readAccount() : accounts;
        accounts.remove(account.username);
        // 写入配置文件
        //  配置文件存在,序列化读取配置文件
        BufferedOutputStream outputStream = FileUtil.getOutputStream(AccountPath);
        IoUtil.writeObj(outputStream,true,accounts);
        IoUtil.close(outputStream);
    }

    public static boolean exist(Account account){
        accounts = accounts == null ? readAccount() : accounts;
        return accounts.containsKey(account.username);
    }

    // 清空全部账号
    public static void clearAccount(){
        accounts = accounts == null ? readAccount() : accounts;
        accounts.clear();
        // 写入配置文件
        //  配置文件存在,序列化读取配置文件
        BufferedOutputStream outputStream = FileUtil.getOutputStream(AccountPath);
        IoUtil.writeObj(outputStream,true,accounts);
        IoUtil.close(outputStream);
    }

    public static void printUserAccount(List<String> accountHistory){
        System.out.println(String.format("您有【%d】个账号记录,请选择要登录的账号...", accountHistory.size()));
        for (int i = 0; i < accountHistory.size(); i++) {
            System.out.println(String.format("| %d. %s", i,accountHistory.get(i)));
        }
        System.out.println(String.format("| %d. 其他账号", accountHistory.size()));
    }

    public static Account InputAccount(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入账号");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        return new Account(username,password);
    }


    // 登录之前要做的事情
    public static Account preLogin(){
        // 历史账号
        HashMap<String, Account> history = AccountManager.readAccount();
        List<String> accountHistory = new ArrayList<>(history.keySet());
        Account account = null;


        // 历史中没有记录
        if (accountHistory.size() == 0){
            account = InputAccount();
        }else{
            // 历史中有记录
            printUserAccount(accountHistory);
            int input = ConsoleUtils.inputNumber(0, accountHistory.size());

            if (input < 0 || input >= accountHistory.size()){
                // 手动输入
                account = InputAccount();
            }else{
                String accountName = accountHistory.get(input);
                account = history.get(accountName);
            }
        }
        return account;
    }


}
